package com.toy.demo;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * 简单实例
 */
public class Operate {

    private static ProcessEngine processEngine =ProcessEngines.getDefaultProcessEngine();

    public static void main(String[] args) {
        Operate operate = new Operate();
//        operate.deployFlow2(operate);
//        operate.flowStart();
        operate.findMyPersonTask();
//        operate.completeTask();
    }

    /**
     * 部署流程图方式一，插入到数据库
     * 生成png文件：修改画好的bpmn文件为xml,格式进行向上保存，会自动生成
     */
    public void deployFlow(){
        ProcessEngine processEngine =
        ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti/activiti.cfg.xml") .buildProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //获取在classpath下的流程文件
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("activiti/leave.zip");
        ZipInputStream zipInputStream = new ZipInputStream(in);
        //使用deploy方法发布流程
        repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("leave")
                .deploy();
    }

    /**
     * 部署方式二
     */
    public void deployFlow2() {
        ProcessEngine processEngine =
                ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti/activiti.cfg.xml") .buildProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment  = processEngine.getRepositoryService()

                .createDeployment()

                .name("myleave")

                .addClasspathResource("activiti/myleave.bpmn")

                .addClasspathResource("activiti/myleave.png")

                .deploy();

        System.out.println(deployment.getId());

        System.out.println(deployment.getName());
    }

    /**
     *     启动流程图
     */
    public void flowStart(){

        RuntimeService runtimeService = processEngine.getRuntimeService();

        //用key启动时按照最新的流程图版本定义启动

        /**
         * key为创建图时候的id
         */
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");

    }

    /**
     * 查询流程
     */
    public void findMyPersonTask(){

        //执行步骤ID
//        String assignee = "cwh";
        String assignee = "menco";

        List<Task> taskList= processEngine.getTaskService()//获取任务service

                .createTaskQuery()//创建查询对象

                .taskAssignee(assignee)//指定查询人

                .list();

        if(taskList.size()>0){

            for (Task task : taskList){

                System.out.println("代办任务ID:"+task.getId());

                System.out.println("代办任务name:"+task.getName());

                System.out.println("代办任务创建时间:"+task.getCreateTime());

                System.out.println("代办任务办理人:"+task.getAssignee());

                System.out.println("流程实例ID:"+task.getProcessInstanceId());

                System.out.println("执行对象ID:"+task.getExecutionId());

            }

        }

    }

    /**
     * 完成某个任务
     */

    public void completeTask(){

        String taskId = "5004";
        processEngine.getTaskService().complete(taskId);//完成任务
        System.out.println("完成任务，任务ID"+taskId);

    }

    public void completeTask1(){

        String taskid = "7502";

        TaskService taskService = processEngine.getTaskService();

        processEngine.getTaskService().complete(taskid);//完成任务

        System.out.println("完成任务，任务ID"+taskid);
    }
}


